package com.chaofan.authsso.core.web;

import cn.hutool.core.util.StrUtil;
import com.chaofan.authsso.core.common.AuthToken;
import com.chaofan.authsso.core.common.Methods;
import com.chaofan.authsso.core.common.SessionAndToken;
import com.chaofan.authsso.core.common.SsoRestfulMessage;
import com.chaofan.authsso.core.service.CacheInstance;
import com.chaofan.authsso.core.util.CacheKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author: Chaofan
 * Date: 2020/6/5 13:50
 * email: chaofan2685@qq.com
 */
@RequestMapping(value = "/authsso/token")
public class TokenController extends Methods {

    @Autowired
    CacheInstance cacheInstance;

    @GetMapping(value = "/check/{token}")
    @ResponseBody
    public SsoRestfulMessage<String> check(@PathVariable String token) {
        //首先查找该token是否需要被替换
        String validToken = (String) cacheInstance.get(CacheKeyUtil.applyTokenKey(token));
        if (StrUtil.isNotEmpty(validToken)){
            return buildApplyToken(new AuthToken(validToken));
        }
        //不需要替换,则开始校验这个token
        Object loginStatus = cacheInstance.get(CacheKeyUtil.tokenKey(token));
        if (loginStatus == null) {
            //如果token不存在,返回8301让client端重新申请token
            return buildNewToken(null);
        } else {
            //如果token存在,则将他的有效期延长1800秒
            cacheInstance.expire(CacheKeyUtil.tokenKey(token),1800);
            return buildSuccessRest(null);
        }
    }

    @GetMapping(value = "/apply/{sessionId}")
    @ResponseBody
    public SsoRestfulMessage<String> apply(@PathVariable String sessionId) {
        AuthToken authToken = null;
        Object authCodeObj = cacheInstance.get(CacheKeyUtil.sessionCodeKey(sessionId));
        if (authCodeObj == null){
            authToken = new AuthToken(cacheInstance);
            SessionAndToken sessionToken = new SessionAndToken(sessionId,authToken.getToken());
            cacheInstance.set(CacheKeyUtil.authCodeKey(authToken.getAuthCode()),sessionToken,600);
            cacheInstance.set(CacheKeyUtil.sessionCodeKey(sessionId),authToken.getAuthCode(),600);
        }else {
            String authCode = authCodeObj.toString();
            SessionAndToken token = (SessionAndToken) cacheInstance.get(CacheKeyUtil.authCodeKey(authCode));
            authToken = new AuthToken(authCode,token.getToken());
            cacheInstance.expire(CacheKeyUtil.authCodeKey(authToken.getAuthCode()),600);
            cacheInstance.expire(CacheKeyUtil.sessionCodeKey(sessionId),600);
        }
        return buildSuccessRest(authToken);
    }

    @GetMapping(value = "/invalidate/{token}")
    @ResponseBody
    public SsoRestfulMessage<String> invalidate(@PathVariable String token) {
        cacheInstance.del(CacheKeyUtil.tokenKey(token));
        return buildSuccessRest(null);
    }

}
